package com.bd.scm.module.authority.controller;

import com.alibaba.fastjson.JSONObject;
import com.bd.scm.domain.dto.authority.AuthorityDto;
import com.bd.scm.module.authority.service.AuthorityService;
import com.bd.scm.module.common.BaseController;
import com.bd.scm.module.enums.ExceptionEmun;
import com.bd.scm.module.enums.ModuleEnum;
import com.bd.utils.vo.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/** 权限管理Controller
 * @ClassName AuthorityController
 * @Description TODO
 * @Author YH
 * @Date 2020/5/7 0007 14:23
 * @Version 1.0
 **/
@RestController
@RequestMapping("/api/authority")
public class AuthorityController extends BaseController {

  private Logger log = LoggerFactory.getLogger("authorityLogger");

  @Autowired
  private AuthorityService authorityService;

  /**
   * 根据登录人分组编号与菜单编号查询权限
   * @param authorityDto groupNo 登录人分组编号 menuBh 菜单编号
   * @return 权限代码
   */
  @PostMapping("findAuthorityMenuSet")
  public Result findAuthorityMenuSet(HttpServletRequest request, @RequestBody AuthorityDto authorityDto) {
    Result result = null;
    JSONObject user = null;
    try{
      ModuleEnum moduleEnum = ModuleEnum.valueOf(authorityDto.getMenuBh().toUpperCase());

      authorityDto.setMenuBh(moduleEnum.getCode());
      user = (JSONObject) request.getSession().getAttribute("user");
      authorityDto.setGroupNo(user.getString("glyGroup"));

      try {
        List<JSONObject> authorityMenuSet = this.authorityService.findAuthorityMenuSet(authorityDto);
        result = Result.ok("获取权限成功");
        result.setResult(authorityMenuSet);
      } catch (Exception e) {
        e.printStackTrace();
        result = Result.error("获取权限失败");
      }

    }catch (NullPointerException n){
      n.printStackTrace();
      result = Result.error("登录信息失效，请重新登录");
    }

    return result;
  }

}
